<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../../stores'
import {userLoginService, userRegisterService} from "@/api/user.js";
import {ElMessage} from "element-plus";

//声明表单
const form = ref({
  account: null,
  password: null,
  confirmPass: null,
  role: null,
  uname: null
})

const router = useRouter()

const userStore = useUserStore()

const formRef = ref()

//声明自定义校验函数
const validatePassword = (rule, confirmPass, callback) => {
  if (confirmPass === '') {
    callback(new Error('请确认密码'))
  } else if (confirmPass !== form.value.password) {
    callback(new Error('两次输入的密码不一致'))
  } else {
    callback()
  }
}

//声明表单校验规则
const rules = {
  account: [
    { required: true, message: '请输入账号', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
  ],
  confirmPass: [
    { validator: validatePassword, trigger: 'blur' },
  ],
  uname: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
  ],
  role: [
    { required: true, message: '请选择角色', trigger: 'change' }
  ]
}

const register = async () => {
  await formRef.value.validate()
  const res = await userRegisterService(form.value.account, form.value.password, form.value.uname, form.value.role)
  if (res.code === '200') {
    ElMessage({
      type: "success",
      message: '注册成功',
    });
    await router.push('/');
  } else {
    ElMessage({
      type: "error",
      message: res.msg,
    });
  }
}

</script>

<template>
  <div style="height: 100vh; display: flex; align-items: center; justify-content: center; background-color: #669fef">
    <div style="display: flex; background-color: white; width: 50%; border-radius: 5px; overflow: hidden">
      <div style="flex: 1">
        <img src="@/assets/imgs/register.png" alt="" style="width: 100%">
      </div>
      <div style="flex: 1; display: flex; align-items: center; justify-content: center">
        <el-form :model="form" style="width: 80%" :rules="rules" ref="formRef">
          <div style="font-size: 20px; font-weight: bold; text-align: center; margin-bottom: 20px">欢迎注册账号
          </div>
          <el-form-item prop="account">
            <el-input prefix-icon="user" size="medium" placeholder="请输入账号" v-model="form.account"></el-input>
          </el-form-item>
          <el-form-item prop="uname">
            <el-input prefix-icon="user" size="medium" placeholder="请输入用户名" v-model="form.uname"></el-input>
          </el-form-item>
          <el-form-item prop="role">
            <el-select v-model="form.role" placeholder="请选择角色" size="medium">
              <el-option label="学生" value="student"></el-option>
              <el-option label="家长" value="parent"></el-option>
              <el-option label="教师" value="teacher"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="password">
            <el-input prefix-icon="lock" size="medium" show-password placeholder="请输入密码"
              v-model="form.password"></el-input>
          </el-form-item>
          <el-form-item prop="confirmPass">
            <el-input prefix-icon="lock" size="medium" show-password placeholder="请确认密码"
              v-model="form.confirmPass"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="info" style="width: 100%; background-color: #00a6ff;" @click="register">注 册</el-button>
          </el-form-item>
          <div style="display: flex">
            <div style="flex: 1">已经有账号了？请 <span style="color: #6e77f2; cursor: pointer"
                @click="$router.push('/')">登录</span></div>
          </div>
        </el-form>
      </div>
    </div>

  </div>
</template>

<style scoped></style>
